﻿using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using ThePresenter.Polls;
using ThePresenter.Polls.Data;

namespace ThePresenter.Windows
{
	/// <summary>
	/// Interaction logic for CreatePollDialog.xaml
	/// </summary>
	public partial class CreatePollDialog : Window
	{
		#region Fields

		public Slide CanvasCandidate = null;

		#endregion

		#region Dependency property identifiers

		/// <summary>
		/// Identifies the <see cref="Poll"/> dependency property.
		/// </summary>
		/// <value>The identifier for the <see cref="Poll"/> dependency property.</value>
		public static readonly DependencyProperty PollProperty = DependencyProperty.Register("Poll", typeof(Poll), typeof(CreatePollDialog));

		/// <summary>
		/// Identifies the <see cref="QuestionData"/> dependency property.
		/// </summary>
		/// <value>The identifier for the <see cref="QuestionData"/> dependency property.</value>
		public static readonly DependencyProperty QuestionDataProperty = DependencyProperty.Register("QuestionData", typeof(QuestionData), typeof(CreatePollDialog));

		/// <summary>
		/// Identifies the <see cref="OptionsData"/> dependency property.
		/// </summary>
		/// <value>The identifier for the <see cref="OptionsData"/> dependency property.</value>
		public static readonly DependencyProperty OptionsDataProperty = DependencyProperty.Register("OptionsData", typeof(OptionsData), typeof(CreatePollDialog));

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the poll. This is a dependency property.
		/// </summary>
		/// <value>The poll.</value>
		public Poll Poll
		{
			get { return (Poll)GetValue(PollProperty); }
			set { SetValue(PollProperty, value); }
		}

		/// <summary>
		/// Gets or sets the type of the question. This is a dependency property.
		/// </summary>
		/// <value>The type of the question.</value>
		public QuestionData QuestionData
		{
			get { return (QuestionData)GetValue(QuestionDataProperty); }
			set { SetValue(QuestionDataProperty, value); }
		}

		/// <summary>
		/// Gets or sets the answer data. This is a dependency property.
		/// </summary>
		/// <value>The answer data.</value>
		public OptionsData OptionsData
		{
			get { return (OptionsData)GetValue(OptionsDataProperty); }
			set { SetValue(OptionsDataProperty, value); }
		} 

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="CreatePollDialog"/> class.
		/// </summary>
		public CreatePollDialog()
		{
			InitializeComponent();
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Handles the Loaded event of the Window control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			Poll = new Poll();

			// QuestionData <==> QuestionPanel.Content.QuestionData
			Binding qd_b = new Binding("Content.QuestionData")
			{
				//Source = this,
				ElementName = "QuestionPanel",
				Mode = BindingMode.TwoWay,
				UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
			};

			this.SetBinding(QuestionDataProperty, qd_b);

			// OptionsData <==> AnswerPanel.Content.AnswerData
			Binding ad_b = new Binding("Content.AnswerData")
			{
				//Source = this,
				ElementName = "AnswerPanel",
				Mode = BindingMode.TwoWay,
				UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
			};

			this.SetBinding(OptionsDataProperty, ad_b);

			// Disable attach
			if (this.CanvasCandidate == null)
			{
				this.CanvasQuestionCheckBox.IsEnabled = false;
				this.CanvasQuestionCheckBox.IsChecked = false;
			}
		}

		/// <summary>
		/// Handles the CanExecute event of the OK command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Input.CanExecuteRoutedEventArgs"/> instance containing the event data.</param>
		private void OK_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			// TODO QuestionData should define bool IsEmpty getter
			
			e.CanExecute = QuestionData != null && OptionsData != null;
			e.Handled = true;
		}

		/// <summary>
		/// Handles the Executed event of the OK command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
		private void OK_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			// TODO Try to bind to the Poll.QuestionData and Poll.AnswerData
			Poll.QuestionData = QuestionData;
			Poll.OptionsData = OptionsData;

			if (this.CanvasQuestionCheckBox.IsChecked == true)
			{
				Poll.QuestionData = new CanvasQuestionData(this.CanvasCandidate) { Question = QuestionData.Question, QuestionType = QuestionType.Canvas };
			}

			DialogResult = true;
			e.Handled = true;
		}

		/// <summary>
		/// Handles the CanExecute event of the Cancel command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Input.CanExecuteRoutedEventArgs"/> instance containing the event data.</param>
		private void Cancel_CanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
			e.Handled = true;
		}

		/// <summary>
		/// Handles the Executed event of the Cancel command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
		private void Cancel_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			DialogResult = false;
			e.Handled = true;
		}

		#endregion
	}
}
